package org.lys.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lys.model.User;
import org.lys.service.UserService;
import org.lys.service.impl.UserServiceImpl;
import org.lys.util.Pagination;
import org.lys.vo.UserVO;

public class UserServlet extends HttpServlet {
	
	private static final long serialVersionUID = -5579992798736868280L;
	private UserService userService = new UserServiceImpl();

	/**
	 * Constructor of the object.
	 */
	public UserServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String kind = request.getParameter("kind");
		UserVO userVO = new UserVO();
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		userVO.setName(name);
		
		if("edit".equals(kind)){
			if(!userVO.validate()){//
				request.setAttribute("errors", userVO.getErrors());
				request.getRequestDispatcher("/user/editUser.jsp").forward(request, response);
			}
			User user = new User();
			user.setName(name);
			
			if("".equals(id)){//新增
				int pk = userService.add(user);
				request.getRequestDispatcher("/UserServlet?kind=view&name=&id="+pk).forward(request, response);
			}else{
				user.setId(Integer.parseInt(id));
				userService.update(user);
				
				request.getRequestDispatcher("/UserServlet?kind=view&name=").forward(request, response);
			}
			
		}else if("view".equals(kind)){
				User user = new User();
				if(!"".equals(id)&&id!=null){
					user.setId(Integer.parseInt(id));	
				}
				user.setName(name);
				
				String pageNow = request.getParameter("pageNow");
				int pageNowInt = 1;
				if(!"".equals(pageNow)&&pageNow!=null){
					pageNowInt = Integer.parseInt(pageNow);
					if(pageNowInt<1){
						pageNowInt = 1;
					}
				}
				
				
				Pagination pagination = new Pagination();
				pagination.setPageNow(pageNowInt);
				pagination.setUrl("UserServlet?kind=view");
				
				pagination = userService.queryPage(user,pagination);
				
				request.setAttribute("users", pagination.getData());
				
				
				request.setAttribute("navigation", pagination.getNavigation());
				
				request.getRequestDispatcher("/user/user.jsp").forward(request, response);
		}else if("delete".equals(kind)){
			
			User user = new User();
			user.setId(Integer.parseInt(id));
			
			userService.delete(user);
			
			request.getRequestDispatcher("/UserServlet?kind=view&id=").forward(request, response);
		}else if("preUpdate".equals(kind)){
			
			User user = new User();
			user.setId(Integer.parseInt(id));
			
			user = userService.get(Integer.parseInt(id));
			
			request.setAttribute("user", user);
			request.getRequestDispatcher("/user/editUser.jsp").forward(request, response);
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
